package cn.itcast.nems.listener.studentrecord;

import cn.hutool.core.lang.Assert;
import cn.itcast.nems.common.tenant.TenantInfo;
import cn.itcast.nems.common.tenant.TenantInfoHolder;
import cn.itcast.nems.listener.NemsMessageListener;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordValidMessageDTO;
import cn.itcast.nems.studentrecord.service.studentrecord.StudentRecordValidService;
import com.alibaba.fastjson.JSON;
import com.esotericsoftware.minlog.Log;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 学籍支付消息监听处理逻辑
 * </p>
 *
 * @author zhouwei
 * @since 2023/12/7 2:08 PM
 **/
@Component
@RequiredArgsConstructor
public class StudentRecordValidMessageListener implements NemsMessageListener {
    private final static String TagName  =  "TAG_STUDENT_RECORD_VALID" ;
    private final StudentRecordValidService studentRecordValidService ;

    @Override
    public String getTagName() {
        return TagName;
    }

    @Override
    public void processMessage(String body) {
        // 监听学籍支付消息
        StudentRecordValidMessageDTO messageDTO = JSON.parseObject(body, StudentRecordValidMessageDTO.class);
        Assert.notNull(messageDTO, "【消息监听】tag: TAG_STUDENT_RECORD_VALID,消息体中业务数据实体为空");
        Assert.notBlank(messageDTO.getStudentRecordId(), "【消息监听】tag: TAG_STUDENT_RECORD_VALID,学籍ID为空");
        TenantInfoHolder.setTenantInfo(new TenantInfo(messageDTO.getTenantId(), messageDTO.getSubtenantId()));
        try {
            studentRecordValidService.writeOff(messageDTO.getStudentRecordId());
        } catch (Exception e) {
            Log.error("【学籍生效】消费逻辑, 学籍ID:{}, 个人优惠单核销失败", messageDTO.getStudentRecordId(), e);
        } finally {
            TenantInfoHolder.clear();
        }
    }
}
